package com.example.examplebus.controller;

import com.example.examplebus.entity.BusRoute;
import com.example.examplebus.service.BusRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/bus-routes")
public class BusRouteController {

    @Autowired
    private BusRouteService busRouteService;

    @GetMapping("/getAll")
    public List<BusRoute> getAllRoutes() {
        return busRouteService.list();
    }

    @GetMapping("/getRouteByNumber/{routeNumber}")
    public BusRoute getRouteByNumber(@PathVariable String routeNumber) {
        return busRouteService.findByRouteNumber(routeNumber);
    }

    @GetMapping("/search")
    public List<BusRoute> searchRoutesByName(@RequestParam String routeName) {
        return busRouteService.findByRouteName(routeName);
    }

    @PostMapping()
    public BusRoute createRoute(@RequestBody BusRoute busRoute) {
        busRouteService.save(busRoute);
        return busRoute;
    }

    @PutMapping("/{id}")
    public BusRoute updateRoute(@PathVariable Long id, @RequestBody BusRoute busRoute) {
        busRoute.setId(id);
        busRouteService.updateById(busRoute);
        return busRoute;
    }

    @DeleteMapping("/{id}")
    public void deleteRoute(@PathVariable Long id) {
        busRouteService.removeById(id);
    }
}